package com.hemaapp.yjnh.chat;

import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import xtom.frame.fileload.XtomFileDownLoader;
import xtom.frame.image.load.XtomImageTask;
import xtom.frame.media.XtomVoicePlayer;
import xtom.frame.media.XtomVoicePlayer.XtomVoicePlayListener;
import xtom.frame.util.XtomToastUtil;
import xtom.frame.view.XtomListView;
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.ClipboardManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.hemaapp.hm_FrameWork.HemaAdapter;
import com.hemaapp.hm_FrameWork.HemaUtil;
import com.hemaapp.hm_FrameWork.chat.ChatConfig;
import com.hemaapp.hm_FrameWork.chat.ChatDBClient;
import com.hemaapp.hm_FrameWork.chat.ChatMessage;
import com.hemaapp.hm_FrameWork.view.RoundedImageView;
import com.hemaapp.hm_FrameWork.view.ShowLargeImageView;
import com.hemaapp.yjnh.BaseUtil;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.activity.VideoPlayActivity;
import com.hemaapp.yjnh.nettask.ImageAvatarTask;
import com.hemaapp.yjnh.view.SoundProgressBar;

/**
 * 私聊
 */
public class ChatPrivateAdapter extends HemaAdapter implements OnClickListener,
		OnLongClickListener {
	private static final int EMPTY = 0;
	private static final int VIEWTYPE_TEXT_MY = 1;
	private static final int VIEWTYPE_TEXT = 2;
	private static final int VIEWTYPE_IMAGE_MY = 3;
	private static final int VIEWTYPE_IMAGE = 4;
	private static final int VIEWTYPE_VOICE_MY = 5;
	private static final int VIEWTYPE_VOICE = 6;
	private static final int VIEWTYPE_VIDEO_MY = 7;
	private static final int VIEWTYPE_VIDEO = 8;

	private Activity activity;
	private View rootView;
	private ArrayList<ChatMessage> messages;
	private XtomListView listView;
	private ChatMessage longMessage;
	private XtomVoicePlayer voicePlayer;

	public ChatPrivateAdapter(Activity activity, View rootView,
			ArrayList<ChatMessage> messages, XtomListView listView) {
		super(activity);
		this.activity = activity;
		this.rootView = rootView;
		this.messages = messages;
		this.listView = listView;
		isTimeVisiable();
	}

	@Override
	public void notifyDataSetChanged() {
		isTimeVisiable();
		super.notifyDataSetChanged();
	}

	private void isTimeVisiable() {
		if (messages == null)
			return;
		String s = null;
		for (ChatMessage message : messages) {
			if (s == null) {
				s = message.getRegdatedatetime();
				message.setTimeVisiable(true);
			} else {
				if (doCha(message.getRegdatedatetime(), s) > 1000 * 60 * 5) {
					s = message.getRegdatedatetime();
					message.setTimeVisiable(true);
				} else {
					message.setTimeVisiable(false);
				}
			}
		}
	}

	// 计算时间差
	private long doCha(String time1, String time2) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
				Locale.getDefault());
		Date d1 = null;
		Date d2 = null;
		try {
			d1 = sdf.parse(time1);
			d2 = sdf.parse(time2);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d1.getTime() - d2.getTime();
	}

	@Override
	public int getCount() {
		int count = getListSize();
		if (count == 0)
			count = 1;
		return count;
	}

	private int getListSize() {
		return messages == null ? 0 : messages.size();
	}

	@Override
	public boolean isEmpty() {
		return getListSize() == 0;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getItemViewType(int position) {
		if (isEmpty())
			return EMPTY;

		ChatMessage message = messages.get(position);
		String from = message.getFromjid();
		String packtype = message.getdxpacktype();// 1：文本 2：图片 3：音频 4：视频
		if (from.equals(ChatConfig.LOGIN_CID)) {
			if ("4".equals(packtype))
				return VIEWTYPE_VIDEO_MY;
			else if ("3".equals(packtype))
				return VIEWTYPE_VOICE_MY;
			else if ("2".equals(packtype))
				return VIEWTYPE_IMAGE_MY;
			else
				return VIEWTYPE_TEXT_MY;
		} else {
			if ("4".equals(packtype))
				return VIEWTYPE_VIDEO;
			else if ("3".equals(packtype))
				return VIEWTYPE_VOICE;
			else if ("2".equals(packtype))
				return VIEWTYPE_IMAGE;
			else
				return VIEWTYPE_TEXT;
		}
	}

	@Override
	public int getViewTypeCount() {
		return 9;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		int type = getItemViewType(position);
		if (type == EMPTY)
			return getEmptyView(parent);

		if (convertView == null) {
			convertView = getView(type);
		}

		setData(position, type, convertView);

		return convertView;
	}

	// 获取视图
	private View getView(int type) {
		View view = null;
		switch (type) {
		case VIEWTYPE_TEXT_MY:
			view = LayoutInflater.from(mContext).inflate(
					R.layout.listitem_chat_text_my, null);
			ViewHolderTextMy holderTextMy = new ViewHolderTextMy();
			findViewTextMy(view, holderTextMy);
			view.setTag(R.id.TAG_VIEWHOLDER, holderTextMy);
			break;
		case VIEWTYPE_TEXT:
			view = LayoutInflater.from(mContext).inflate(
					R.layout.listitem_chat_text, null);
			ViewHolderText holderText = new ViewHolderText();
			findViewText(view, holderText);
			view.setTag(R.id.TAG_VIEWHOLDER, holderText);
			break;
		case VIEWTYPE_IMAGE_MY:
			view = LayoutInflater.from(mContext).inflate(
					R.layout.listitem_chat_image_my, null);
			ViewHolderImageMy holderImageMy = new ViewHolderImageMy();
			findViewImageMy(view, holderImageMy);
			view.setTag(R.id.TAG_VIEWHOLDER, holderImageMy);
			break;
		case VIEWTYPE_IMAGE:
			view = LayoutInflater.from(mContext).inflate(
					R.layout.listitem_chat_image, null);
			ViewHolderImage holderImage = new ViewHolderImage();
			findViewImage(view, holderImage);
			view.setTag(R.id.TAG_VIEWHOLDER, holderImage);
			break;
		case VIEWTYPE_VOICE_MY:
			view = LayoutInflater.from(mContext).inflate(
					R.layout.listitem_chat_voice_my, null);
			ViewHolderVoiceMy holderVoiceMy = new ViewHolderVoiceMy();
			findViewVoiceMy(view, holderVoiceMy);
			view.setTag(R.id.TAG_VIEWHOLDER, holderVoiceMy);
			break;
		case VIEWTYPE_VOICE:
			view = LayoutInflater.from(mContext).inflate(
					R.layout.listitem_chat_voice, null);
			ViewHolderVoice holderVoice = new ViewHolderVoice();
			findViewVoice(view, holderVoice);
			view.setTag(R.id.TAG_VIEWHOLDER, holderVoice);
			break;
		case VIEWTYPE_VIDEO_MY:
			view = LayoutInflater.from(mContext).inflate(
					R.layout.listitem_chat_video_my, null);
			ViewHolderVideoMy holderVideoMy = new ViewHolderVideoMy();
			findViewVideoMy(view, holderVideoMy);
			view.setTag(R.id.TAG_VIEWHOLDER, holderVideoMy);
			break;
		case VIEWTYPE_VIDEO:
			view = LayoutInflater.from(mContext).inflate(
					R.layout.listitem_chat_video, null);
			ViewHolderVideo holderVideo = new ViewHolderVideo();
			findViewVideo(view, holderVideo);
			view.setTag(R.id.TAG_VIEWHOLDER, holderVideo);
			break;
		}
		return view;
	}

	// 查找我发送的视频消息View
	private void findViewVideoMy(View view, ViewHolderVideoMy holder) {
		findViewNormalMy(view, holder);
		holder.content = view.findViewById(R.id.videocontent);
		holder.duration = (TextView) view.findViewById(R.id.duration);
	}

	// 查找视频消息View
	private void findViewVideo(View view, ViewHolderVideo holder) {
		findViewNormal(view, holder);
		holder.content = view.findViewById(R.id.videocontent);
		holder.duration = (TextView) view.findViewById(R.id.duration);
	}

	// 查找我发送的语音消息View
	private void findViewVoiceMy(View view, ViewHolderVoiceMy holder) {
		findViewNormalMy(view, holder);
		holder.content = view.findViewById(R.id.voicecontent);
		holder.soundbar = (SoundProgressBar) view.findViewById(R.id.soundbar);
		holder.duration = (TextView) view.findViewById(R.id.duration);
	}

	// 查找语音消息View
	private void findViewVoice(View view, ViewHolderVoice holder) {
		findViewNormal(view, holder);
		holder.content = view.findViewById(R.id.voicecontent);
		holder.soundbar = (SoundProgressBar) view.findViewById(R.id.soundbar);
		holder.duration = (TextView) view.findViewById(R.id.duration);
	}

	// 查找我发送的图片消息View
	private void findViewImageMy(View view, ViewHolderImageMy holder) {
		findViewNormalMy(view, holder);
		holder.content = view.findViewById(R.id.imagecontent);
		holder.imageview = (ImageView) view.findViewById(R.id.imageview);
	}

	// 查找图片消息View
	private void findViewImage(View view, ViewHolderImage holder) {
		findViewNormal(view, holder);
		holder.content = view.findViewById(R.id.imagecontent);
		holder.imageview = (ImageView) view.findViewById(R.id.imageview);
	}

	// 查找我发送的文本消息View
	private void findViewTextMy(View view, ViewHolderTextMy holder) {
		findViewNormalMy(view, holder);
		holder.content = (TextView) view.findViewById(R.id.content);
	}

	// 查找文本消息View
	private void findViewText(View view, ViewHolderText holder) {
		findViewNormal(view, holder);
		holder.content = (TextView) view.findViewById(R.id.content);
	}

	// 查找我发送的通用View
	private void findViewNormalMy(View view, ViewHolderNormalMy holder) {
		findViewNormal(view, holder);
		holder.progressBar = (ProgressBar) view.findViewById(R.id.progressbar);
		holder.failed = (ImageButton) view.findViewById(R.id.failed);
	}

	// 查找通用View
	private void findViewNormal(View view, ViewHolderNormal holder) {
		holder.time = (TextView) view.findViewById(R.id.time);
		holder.avatar = (RoundedImageView) view.findViewById(R.id.avatar);
		holder.avatar.setCornerRadius(1000);
	}

	// 设置数据
	private void setData(int position, int type, View convertView) {
		switch (type) {
		case VIEWTYPE_TEXT_MY:
			ViewHolderTextMy holderTextMy = (ViewHolderTextMy) convertView
					.getTag(R.id.TAG_VIEWHOLDER);
			setDataTextMy(position, holderTextMy);
			break;
		case VIEWTYPE_TEXT:
			ViewHolderText holderText = (ViewHolderText) convertView
					.getTag(R.id.TAG_VIEWHOLDER);
			setDataText(position, holderText);
			break;
		case VIEWTYPE_IMAGE_MY:
			ViewHolderImageMy holderImageMy = (ViewHolderImageMy) convertView
					.getTag(R.id.TAG_VIEWHOLDER);
			setDataImageMy(position, holderImageMy);
			break;
		case VIEWTYPE_IMAGE:
			ViewHolderImage holderImage = (ViewHolderImage) convertView
					.getTag(R.id.TAG_VIEWHOLDER);
			setDataImage(position, holderImage);
			break;
		case VIEWTYPE_VOICE_MY:
			ViewHolderVoiceMy holderVoiceMy = (ViewHolderVoiceMy) convertView
					.getTag(R.id.TAG_VIEWHOLDER);
			setDataVoiceMy(position, holderVoiceMy);
			break;
		case VIEWTYPE_VOICE:
			ViewHolderVoice holderVoice = (ViewHolderVoice) convertView
					.getTag(R.id.TAG_VIEWHOLDER);
			setDataVoice(position, holderVoice);
			break;
		case VIEWTYPE_VIDEO_MY:
			ViewHolderVideoMy holderVideoMy = (ViewHolderVideoMy) convertView
					.getTag(R.id.TAG_VIEWHOLDER);
			setDataVideoMy(position, holderVideoMy);
			break;
		case VIEWTYPE_VIDEO:
			ViewHolderVideo holderVideo = (ViewHolderVideo) convertView
					.getTag(R.id.TAG_VIEWHOLDER);
			setDataVideo(position, holderVideo);
			break;
		}
	}

	// 设置我发送的视频消息数据
	private void setDataVideoMy(int position, ViewHolderVideoMy holder) {
		ChatMessage message = messages.get(position);
		setDataNormalMy(position, message, holder);
		holder.content.setTag(R.id.TAG, message);
		holder.content.setOnLongClickListener(this);
		holder.content.setOnClickListener(this);
		holder.duration.setText(message.getdxdetail() + "''");
	}

	// 设置视频消息数据
	private void setDataVideo(int position, ViewHolderVideo holder) {
		ChatMessage message = messages.get(position);
		setDataNormal(position, message, holder);
		holder.content.setTag(R.id.TAG, message);
		holder.content.setOnLongClickListener(this);
		holder.content.setOnClickListener(this);
		holder.duration.setText(message.getdxdetail() + "''");
	}

	// 设置我发送的语音消息数据
	private void setDataVoiceMy(int position, ViewHolderVoiceMy holder) {
		ChatMessage message = messages.get(position);
		setDataNormalMy(position, message, holder);
		holder.content.setTag(R.id.TAG, message);
		holder.content.setTag(holder.soundbar);
		holder.content.setOnLongClickListener(this);
		holder.content.setOnClickListener(this);
		holder.duration.setText(message.getdxdetail() + "''");
		if (message.isVoicePlaying()) {
			holder.soundbar.start();
		} else {
			holder.soundbar.stop();
		}
	}

	// 设置语音消息数据
	private void setDataVoice(int position, ViewHolderVoice holder) {
		ChatMessage message = messages.get(position);
		setDataNormal(position, message, holder);
		holder.content.setTag(R.id.TAG, message);
		holder.content.setTag(holder.soundbar);
		holder.content.setOnLongClickListener(this);
		holder.content.setOnClickListener(this);
		holder.duration.setText(message.getdxdetail() + "''");
		if (message.isVoicePlaying()) {
			holder.soundbar.start();
		} else {
			holder.soundbar.stop();
		}
	}

	// 设置我发送的图片消息数据
	private void setDataImageMy(int position, ViewHolderImageMy holder) {
		ChatMessage message = messages.get(position);
		setDataNormalMy(position, message, holder);
		holder.content.setTag(R.id.TAG, message);
		holder.content.setOnLongClickListener(this);
		holder.content.setOnClickListener(this);
		try {
			String path = message.getStanza();
			URL url = new URL(path);
			XtomImageTask task = new XtomImageTask(holder.imageview, url,
					mContext);
			listView.addTask(position, 1, task);
		} catch (MalformedURLException e) {
			holder.imageview.setImageBitmap(null);
		}
	}

	// 设置图片消息数据
	private void setDataImage(int position, ViewHolderImage holder) {
		ChatMessage message = messages.get(position);
		setDataNormal(position, message, holder);
		holder.content.setTag(R.id.TAG, message);
		holder.content.setOnLongClickListener(this);
		holder.content.setOnClickListener(this);
		try {
			String path = message.getStanza();
			URL url = new URL(path);
			XtomImageTask task = new XtomImageTask(holder.imageview, url,
					mContext);
			listView.addTask(position, 1, task);
		} catch (MalformedURLException e) {
			holder.imageview.setImageBitmap(null);
		}
	}

	// 设置我发送的文本消息数据
	private void setDataTextMy(int position, ViewHolderTextMy holder) {
		ChatMessage message = messages.get(position);
		setDataNormalMy(position, message, holder);
		holder.content.setTag(R.id.TAG, message);
		holder.content.setOnLongClickListener(this);
		holder.content.setOnClickListener(this);
		HemaUtil.SetMessageTextView(mContext, holder.content,
				message.getStanza());
	}

	// 设置文本消息数据
	private void setDataText(int position, ViewHolderText holder) {
		ChatMessage message = messages.get(position);
		setDataNormal(position, message, holder);
		holder.content.setTag(R.id.TAG, message);
		holder.content.setOnLongClickListener(this);
		holder.content.setOnClickListener(this);
		HemaUtil.SetMessageTextView(mContext, holder.content,
				message.getStanza());
	}

	// 设置我发送的通用数据
	private void setDataNormalMy(int position, ChatMessage message,
			ViewHolderNormalMy holder) {
		setDataNormal(position, message, holder);
		if ("1".equals(message.getIssend())) {// 发送成功
			holder.progressBar.setVisibility(View.GONE);
			holder.failed.setVisibility(View.GONE);
		} else if ("2".equals(message.getIssend())) {// 发送中
			holder.progressBar.setVisibility(View.VISIBLE);
			holder.failed.setVisibility(View.GONE);
		} else {// 发送失败
			holder.progressBar.setVisibility(View.GONE);
			holder.failed.setVisibility(View.VISIBLE);
			holder.failed.setTag(message);
			holder.failed.setOnClickListener(this);
		}
	}

	// 设置通用数据
	private void setDataNormal(int position, ChatMessage message,
			ViewHolderNormal holder) {
//		if (message.isTimeVisiable()) {
//			holder.time.setText(BaseUtil.transTimeChat(message
//					.getRegdatedatetime()));
//			holder.time.setVisibility(View.VISIBLE);
//		} else
			holder.time.setVisibility(View.GONE);
		try {
			String path = message.getdxavatar();
			URL url = new URL(path);
			ImageAvatarTask task = new ImageAvatarTask(holder.avatar, url, mContext, listView, null);
			listView.addTask(position, 0, task);
		} catch (MalformedURLException e) {
			holder.avatar.setImageResource(R.drawable.default_rabbit_grey);
		}
		holder.avatar.setTag(R.id.TAG, message);
		holder.avatar.setOnClickListener(this);
		String nickname = message.getdxnickname();
		if (nickname.length() > 4)
			nickname = nickname.substring(0, 3) + "...";
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.avatar:
//			ChatMessage message = (ChatMessage) v.getTag(R.id.TAG);
//			Intent it = new Intent(mContext, ClientInfoActivity.class);
//			it.putExtra("id", message.getFromjid());
//			mContext.startActivity(it);
			break;
		case R.id.failed:
			ChatMessage failedMessage = (ChatMessage) v.getTag();
			((ChatPrivateActivity) mContext).sendAgain(failedMessage);
			break;
		case R.id.imagecontent:
			ChatMessage lMessage = (ChatMessage) v.getTag(R.id.TAG);
			String imgurlbig = lMessage.getdxdetail();
			ShowLargeImageView mView = new ShowLargeImageView(activity,
					rootView);
			mView.show();
			mView.setImageURL(imgurlbig);
			break;
		case R.id.voicecontent:
			voicePlay(v);
			break;
		case R.id.videocontent:
			ChatMessage videoMessage = (ChatMessage) v.getTag(R.id.TAG);
			Intent itPlay = new Intent(mContext, VideoPlayActivity.class);
			itPlay.putExtra("url", videoMessage.getStanza());
			mContext.startActivity(itPlay);
			break;
		case R.id.content:
			break;
		default:
			break;
		}

	}

	// 播放语音
	private void voicePlay(View v) {
		ChatMessage voiceMessage = (ChatMessage) v.getTag(R.id.TAG);
		SoundProgressBar soundbar = (SoundProgressBar) v.getTag();
		String path = voiceMessage.getStanza();
		if (isNull(path))
			return;

		if (voicePlayer != null) {
			String oldPath = voicePlayer.getPath();
			if (path.equals(oldPath)) {
				if (voicePlayer.isPlaying())
					voicePlayer.stop();
				else
					voicePlayer.start();
			} else {
				voicePlayer.release();
				voicePlayer = new XtomVoicePlayer(activity, path);
				voicePlayer.setXtomVoicePlayListener(new VoicePlayerListener(
						soundbar, voiceMessage));
				voicePlayer.start();
			}
		} else {
			voicePlayer = new XtomVoicePlayer(activity, path);
			voicePlayer.setXtomVoicePlayListener(new VoicePlayerListener(
					soundbar, voiceMessage));
			voicePlayer.start();
		}
	}

	@Override
	public boolean onLongClick(View v) {
		longMessage = (ChatMessage) v.getTag(R.id.TAG);
		showLongDialog();
		return true;
	}

	public void showLongDialog() {
		String title = longMessage.getdxnickname();
		String packtype = longMessage.getdxpacktype();// 1：文本 2：图片 3：音频 4：视频
		Builder builder = new Builder(mContext);
		if ("1".equals(packtype)) {
			String[] items = { "复制", "删除", "取消" };
			builder.setItems(items, new DialogClickListener(packtype));
		} else {
			String[] items = { "删除", "取消" };
			builder.setItems(items, new DialogClickListener(packtype));
		}
		builder.setTitle(title);
		builder.show();
	}

	// 长按弹窗点击监听
	private class DialogClickListener implements
			DialogInterface.OnClickListener {
		private String packtype;

		public DialogClickListener(String packtype) {
			this.packtype = packtype;
		}

		private void delete() {
			ChatDBClient client = ChatDBClient.get(mContext);
			client.delete(longMessage);
			messages.remove(longMessage);
			notifyDataSetChanged();
		}

		@SuppressWarnings("deprecation")
		private void copy() {
			ClipboardManager cbm = (ClipboardManager) mContext
					.getSystemService(Context.CLIPBOARD_SERVICE);
			cbm.setText(longMessage.getStanza());
		}

		@Override
		public void onClick(DialogInterface dialog, int which) {
			switch (which) {
			case 0:
				if ("1".equals(packtype)) {
					copy();
				} else {
					delete();
				}
				break;
			case 1:
				if ("1".equals(packtype)) {
					delete();
				} else {
					// nothing
				}
				break;
			case 2:
				// nothing
				break;
			}
		}
	}

	// 我发送的视频消息视图Holder
	private static class ViewHolderVideoMy extends ViewHolderNormalMy {
		View content;
		TextView duration;
	}

	// 视频消息视图Holder
	private static class ViewHolderVideo extends ViewHolderNormal {
		View content;
		TextView duration;
	}

	// 我发送的语音消息视图Holder
	private static class ViewHolderVoiceMy extends ViewHolderNormalMy {
		View content;
		SoundProgressBar soundbar;
		TextView duration;
	}

	// 语音消息视图Holder
	private static class ViewHolderVoice extends ViewHolderNormal {
		View content;
		SoundProgressBar soundbar;
		TextView duration;
	}

	// 我发送的图片消息视图Holder
	private static class ViewHolderImageMy extends ViewHolderNormalMy {
		View content;
		ImageView imageview;
	}

	// 图片消息视图Holder
	private static class ViewHolderImage extends ViewHolderNormal {
		View content;
		ImageView imageview;
	}

	// 我发送的文本消息视图Holder
	private static class ViewHolderTextMy extends ViewHolderNormalMy {
		TextView content;
	}
	// 文本消息视图Holder
	
	private static class ViewHolderText extends ViewHolderNormal {
		TextView content;
	}

	// 我发送的通用视图Holder
	private static class ViewHolderNormalMy extends ViewHolderNormal {
		ProgressBar progressBar;
		ImageButton failed;
	}

	// 通用视图Holder
	private static class ViewHolderNormal {
		TextView time;
		RoundedImageView avatar;
//		TextView nickname;
//		TextView worker;
//		TextView regdate;
	}

	// 语音播放监听
	private class VoicePlayerListener implements XtomVoicePlayListener {

		SoundProgressBar soundbar;
		ChatMessage message;

		public VoicePlayerListener(SoundProgressBar soundbar,
				ChatMessage message) {
			this.soundbar = soundbar;
			this.message = message;
		}

		@Override
		public void loadStart(XtomVoicePlayer player, XtomFileDownLoader loader) {
			((ChatPrivateActivity) mContext).showProgressDialog("请稍候");
		}

		@Override
		public void loading(XtomVoicePlayer player, XtomFileDownLoader loader) {
			// TODO Auto-generated method stub

		}

		@Override
		public void loadSuccess(XtomVoicePlayer player,
				XtomFileDownLoader loader) {
			((ChatPrivateActivity) mContext).cancelProgressDialog();
			player.start();
		}

		@Override
		public void loadFailed(XtomVoicePlayer player, XtomFileDownLoader loader) {
			((ChatPrivateActivity) mContext).cancelProgressDialog();
			XtomToastUtil.showLongToast(mContext, "语音文件缓冲失败");
		}

		@Override
		public void onStart(XtomVoicePlayer player) {
			message.setVoicePlaying(true);
			soundbar.start();
		}

		@Override
		public void onPause(XtomVoicePlayer player) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPlaying(XtomVoicePlayer player) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onStop(XtomVoicePlayer player) {
			message.setVoicePlaying(false);
			soundbar.stop();
		}

		@Override
		public void onComplete(XtomVoicePlayer player) {
			message.setVoicePlaying(false);
			soundbar.stop();
		}

		@Override
		public void onError(XtomVoicePlayer player) {
			message.setVoicePlaying(false);
			soundbar.stop();
			XtomToastUtil.showLongToast(mContext, "语音文件播放失败");
		}

	}

	/**
	 * @return the voicePlayer
	 */
	public XtomVoicePlayer getVoicePlayer() {
		return voicePlayer;
	}

}
